package org.msss.sprites;

import org.lwjgl.input.Keyboard;
import org.msss.gamebasic.*;

public class Moving extends Sprite {

	private final int RUN_WAIT = Frame.FRAMERATE / 7;
	private int leftTimer, rightTimer, upTimer, downTimer;
	private int leftRun, rightRun, upRun, downRun;

	public void create() {
		leftTimer = 0;
		rightTimer = 0;
		upTimer = 0;
		downTimer = 0;

		leftRun = 0;
		rightRun = 0;
		upRun = 0;
		downRun = 0;
	}

	@Override
	public void pressed(int keyPressed) {
		// LEFT
		if (keyPressed == Keyboard.KEY_LEFT) {
			if (leftTimer >= Timer.getStepTime() - RUN_WAIT) {
				leftRun = 2;

			} else {
				leftRun = 1;
			}

		}
		// RIGHT
		else if (keyPressed == Keyboard.KEY_RIGHT) {
			if (rightTimer >= Timer.getStepTime() - RUN_WAIT) {
				rightRun = 2;

			} else {
				rightRun = 1;
			}

		}
		// DOWN
		else if (keyPressed == Keyboard.KEY_DOWN) {
			if (downTimer >= Timer.getStepTime() - RUN_WAIT) {
				downRun = 2;

			} else {
				downRun = 1;
			}

		}
		// UP
		else if (keyPressed == Keyboard.KEY_UP) {
			if (upTimer >= Timer.getStepTime() - RUN_WAIT) {
				upRun = 2;
			} else {
				upRun = 1;
			}
		}
	}

	@Override
	public void released(int keyReleased) {
		// LEFT
		if (keyReleased == Keyboard.KEY_LEFT) {
			leftRun = 0;

		}
		// RIGHT
		else if (keyReleased == Keyboard.KEY_RIGHT) {
			rightRun = 0;

		}
		// DOWN
		else if (keyReleased == Keyboard.KEY_DOWN) {
			downRun = 0;

		}
		// UP
		else if (keyReleased == Keyboard.KEY_UP) {
			upRun = 0;

		}
	}

	public void down(int keyDown) {
		if (keyDown == Keyboard.KEY_DOWN) {
			downTimer = Timer.getStepTime();
		} else if (keyDown == Keyboard.KEY_LEFT) {
			leftTimer = Timer.getStepTime();
		} else if (keyDown == Keyboard.KEY_UP) {
			upTimer = Timer.getStepTime();
		} else if (keyDown == Keyboard.KEY_RIGHT) {
			rightTimer = Timer.getStepTime();
		}
	}

	public int getLeftRun() {
		return leftRun;
	}

	public int getRightRun() {
		return rightRun;
	}

	public int getUpRun() {
		return upRun;
	}

	public int getDownRun() {
		return downRun;
	}
}
